﻿// -----------------------------------------------------------------------
// <copyright file="FileTypeIcon.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.Widgets
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class FileTypeIcon : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the FileTypeIcon class.
        /// </summary>
        public FileTypeIcon()
        {
            Core.Player.Events.Instance.Play += new EventHandler(this.OnPlay);

            // Set the value of the double-buffering style bits to true.
            this.DoubleBuffered = true;
            this.UpdateStyles();
        }

        /// <summary>
        /// Occurs when a new media file is played
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPlay(object sender, EventArgs e)
        {
            try
            {
                if (!System.IO.File.Exists(Core.Player.MediaInfo.FilePath))
                {
                    return;
                }

                // Change icon
                string extension = System.IO.Path.GetExtension(Core.Player.MediaInfo.FilePath).Remove(0, 1);
                string file = Metal.Config.Paths.IconsFolder + extension + ".ico";

                if (System.IO.File.Exists(file))
                {
                    this.BackgroundImage = Image.FromFile(file);
                }
                else
                {
                    this.BackgroundImage = Image.FromFile(Metal.Config.Paths.IconsFolder + "metal-player.ico");
                }
            }
            catch(Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Apply our custom image:
            this.BackgroundImage = Image.FromFile(Metal.Config.Paths.IconsFolder + "metal-player.ico");
        }
    }
}
